************************************
*** FIGURE 1: Number of Patients In Care by Age
*** FIGURE A1: EMR Implementation by Year in Malawi
*** FIGURE A2: Descriptive Statistics by Gender and Age
*** FIGURE A4: Distribution of Patient Outcomes
*** FIGURE A9 PANEL B

*** TABLE A3: Descriptive Statistics 
************************************



***FIGURE 1

use Data/Analysis/Data_Individual, clear

gen Year=year(VisitDate)

* Considering the lowerbound of patients age in the year
collapse (min) Age, by(PatientId Year)
drop if Age == .
gen age_cat = 0
	replace age_cat = 1 if Age > 9
	replace age_cat = 2 if Age >17
	replace age_cat = 3 if Age >49
	replace age_cat = . if Age==.
tabulate age_cat, generate(Age_)
rename Age_1 Age_09
rename Age_2 Age_1017
rename Age_3 Age_1849
rename Age_4 Age_50plus

gen check = Age_09+ Age_1017+ Age_1849+ Age_50plus
tab check

collapse (sum) Age_* (count) (PatientId), by(Year)
gen check = Age_09+ Age_1017+ Age_1849+ Age_50plus
gen doublecheck = 1 if PatientId == check
rename PatientId All
drop check doublecheck

gen Age_09_cum = Age_09
gen Age_1017_cum = Age_09 + Age_1017
gen Age_1849_cum = Age_09 + Age_1017 + Age_1849
gen Age_50plus_cum = Age_09 + Age_1017 + Age_1849 + Age_50plus

* Create the stacked area graph
twoway (area Age_50plus_cum Year, color(red)) ///
    (area Age_1849_cum Year, color(green)) ///
    (area Age_1017_cum Year, color(orange)) ///
    (area Age_09_cum Year, color(blue)) if Year <2019, ///
    ytitle("Number of Patients") xtitle("Year") ///
    legend(order(1 "Age 50+" 2 "Age 18-49" 3 "Age 10-17" 4 "Age 0-9")) ///
	note("Note: the plot does not include data from year 2019.") 
		graph save "Output/Figures/Figure01.gph", replace
		graph export "Output/Figures/Figure01.png", replace

		
		
***FIGURE A1


use Data/Raw/malawi_county_shape, clear

ssc install geo2xy, replace     
ssc install palettes, replace       
ssc install colrspace, replace
ssc install schemepack, replace

colorpalette viridis, n(13) nograph reverse
local colors `r(p)'

spmap using "Data/Raw/malawi_county_coord.dta", id(_ID) fcolor(gs13 ..) ocolor(gs15) title ("EMR Clinics by Year", size(*1.5))  subtitle("by Malawi Districts", size(*1)) ///
point(data("Data/Prep/MapDates.dta") ycoord(longitude) xcoord(latitude) fcolor("`colors'") si(medium) by(EMRy) legenda(on)) legend(title("EMR Year") size(*2)) ///
note("Note: data from Malawi Ministry of Health.", size(*1)) 
graph save Graph "Output/Figures/FigureA01.gph", replace
graph export "Output/Figures/FigureA01.png", as(png) replace


***FIGURE A2

use Data/Analysis/Data_IndividualInit, clear


gen LostYearPre=(EMRDate-LastRefPreDate>365)
replace LostYearPre=1 if LastRefPreDate==.
replace LostYearPre=. if YearsPostEMR>-2

replace Default=. if VisitDate<=EMRDate-90
tab Default


gen Died2y=0
replace Died2y=1 if Death==1 & (DeathDate-InitDate<(2*365))

 preserve 
	collapse (mean) meanDefault=Default meanLostYearPre=LostYearPre meanDied2y=Died2y (sd) sdDefault=Default sdLostYearPre=LostYearPre sdDied2y=Died2y (count) nDefault=Default nLostYearPre=LostYearPre nDied2y=Died2y, by(Male)
	
	generate hiDefault = meanDefault + invttail(nDefault-1,0.025)*(sdDefault / sqrt(nDefault))
	generate lowDefault = meanDefault - invttail(nDefault-1,0.025)*(sdDefault / sqrt(nDefault))
	
	generate hiLostYearPre = meanLostYearPre + invttail(nLostYearPre-1,0.025)*(sdLostYearPre / sqrt(nLostYearPre))
	generate lowLostYearPre = meanLostYearPre - invttail(nLostYearPre-1,0.025)*(sdLostYearPre / sqrt(nLostYearPre))

	generate hiDied2y = meanDied2y + invttail(nDied2y-1,0.025)*(sdDied2y / sqrt(nDied2y))
	generate lowDied2y = meanDied2y - invttail(nDied2y-1,0.025)*(sdDied2y / sqrt(nDied2y))
	
	replace Male = 3 if Male==1
	replace Male = 1 if Male==0

	graph twoway 		(bar meanDefault Male if Male==1, bcolor(red)) ///
	(bar meanDefault Male  if Male==3, bcolor(green)) ///
	(rcap hiDefault lowDefault Male, color(black)), ///
	legend(off) ytitle("In percent", size(mediumlarge)) ylabel(0(0.1)0.3) ///
	xlabel(1 "Female" 3 "Male") xtitle("Gender", size(mediumlarge)) graphregion(color(white))
		graph save "Output/Figures/FigureA2PanelAi.gph", replace
		graph export "Output/Figures/FigureA2PanelAi.png", replace
		
	graph twoway (bar meanLostYearPre Male if Male==1, bcolor(red)) ///
	(bar meanLostYearPre Male  if Male==3, bcolor(green)) ///
	(rcap hiLostYearPre lowLostYearPre Male, color(black)), ///
	legend(off) ytitle("In percent", size(mediumlarge)) ylabel(0(0.1)1) ///
	xlabel(1 "Female" 3 "Male") xtitle("Gender", size(mediumlarge)) graphregion(color(white)) 
		graph save "Output/Figures/FigureA2PanelBi.gph", replace
		graph export "Output/Figures/FigureA2PanelBi.png", replace
		
	graph twoway 	(bar meanDied2y Male if Male==1, bcolor(red)) ///
	(bar meanDied2y Male  if Male==3, bcolor(green)) ///
	(rcap hiDied2y lowDied2y Male, color(black)), ///
	legend(off) ytitle("In percent", size(mediumlarge)) ylabel(0(0.01)0.1) ///
	xlabel(1 "Female" 3 "Male") xtitle("Gender", size(mediumlarge)) graphregion(color(white)) 
		graph save "Output/Figures/FigureA2PanelCi.gph", replace
		graph export "Output/Figures/FigureA2PanelCi.png", replace
	
restore


gen age_cat = 0
	replace age_cat = 1 if Age > 9
	replace age_cat = 2 if Age >17
	replace age_cat = 3 if Age >49
	replace age_cat = . if Age==.
	drop if age_cat ==.
	
preserve 	
	collapse  (mean) meanDefault=Default meanLostYearPre=LostYearPre meanDied2y=Died2y (sd) sdDefault=Default sdLostYearPre=LostYearPre sdDied2y=Died2y (count) nDefault=Default nLostYearPre=LostYearPre nDied2y=Died2y, by(age_cat)
	
	generate hiDefault = meanDefault + invttail(nDefault-1,0.025)*(sdDefault / sqrt(nDefault))
	generate lowDefault = meanDefault - invttail(nDefault-1,0.025)*(sdDefault / sqrt(nDefault))
	
	generate hiLostYearPre = meanLostYearPre + invttail(nLostYearPre-1,0.025)*(sdLostYearPre / sqrt(nLostYearPre))
	generate lowLostYearPre = meanLostYearPre - invttail(nLostYearPre-1,0.025)*(sdLostYearPre / sqrt(nLostYearPre))

	generate hiDied2y = meanDied2y + invttail(nDied2y-1,0.025)*(sdDied2y / sqrt(nDied2y))
	generate lowDied2y = meanDied2y - invttail(nDied2y-1,0.025)*(sdDied2y / sqrt(nDied2y))
	
	graph twoway (bar meanDefault age_cat if age_cat == 0, bcolor(blue))  ///
	(bar meanDefault age_cat if age_cat == 1, bcolor(orange)) ///
	(bar meanDefault age_cat if age_cat == 2, bcolor(green)) ///
	(bar meanDefault age_cat if age_cat == 3, bcolor(red)) (rcap hiDefault lowDefault age_cat, color(black)), ///
	legend(off) xlabel(0 "{&le} 9" 1 "10-17" 2 "18-49" 3 "{&ge} 50") xtitle("Age Category", size(mediumlarge)) ///
	ytitle("In percent", size(mediumlarge)) ylabel(0(0.1)0.3) graphregion(color(white))
		graph save "Output/Figures/FigureA2PanelAii.gph", replace
		graph export "Output/Figures/FigureA2PanelAii.png", replace

	graph twoway (bar meanLostYearPre age_cat if age_cat == 0, bcolor(blue))  ///
	(bar meanLostYearPre age_cat if age_cat == 1, bcolor(orange)) ///
	(bar meanLostYearPre age_cat if age_cat == 2, bcolor(green)) ///
	(bar meanLostYearPre age_cat if age_cat == 3, bcolor(red)) (rcap hiLostYearPre lowLostYearPre age_cat, color(black)), ///
	legend(off) xlabel(0 "{&le} 9" 1 "10-17" 2 "18-49" 3 "{&ge} 50") xtitle("Age Category", size(mediumlarge)) ///
	ytitle("In percent", size(mediumlarge)) ylabel(0(0.1)1) graphregion(color(white))
		graph save "Output/Figures/FigureA2PanelBii.gph", replace
		graph export "Output/Figures/FigureA2PanelBii.png", replace
	
		graph twoway (bar meanDied2y age_cat if age_cat == 0, bcolor(blue))  ///
	(bar meanDied2y age_cat if age_cat == 1, bcolor(orange)) ///
	(bar meanDied2y age_cat if age_cat == 2, bcolor(green)) ///
	(bar meanDied2y age_cat if age_cat == 3, bcolor(red)) (rcap hiDied2y lowDied2y age_cat, color(black)), ///
	legend(off) xlabel(0 "{&le} 9" 1 "10-17" 2 "18-49" 3 "{&ge} 50") xtitle("Age Category", size(mediumlarge)) ///
	ytitle("In percent", size(mediumlarge)) ylabel(0(0.01)0.1) graphregion(color(white))
		graph save "Output/Figures/FigureA2PanelCii.gph", replace
		graph export "Output/Figures/FigureA2PanelCii.png", replace
		
restore

***FIGURE A4

use Data/Analysis/Data_ClinicYears, clear

histogram Death, xtitle("Patient Deaths (count)")
graph save "Output/Figures/FigureA04PanelA.gph", replace
graph export "Output/Figures/FigureA04PanelA.png", replace

histogram InCare, xtitle("Patients In Care (count)")
graph save "Output/Figures/FigureA04PanelB.gph", replace
graph export "Output/Figures/FigureA04PanelB.png", replace

histogram New, xtitle("New Patients (count)")
graph save "Output/Figures/FigureA04PanelC.gph", replace
graph export "Output/Figures/FigureA04PanelC.png", replace

histogram Return, xtitle("Patients Retention (count)")
graph save "Output/Figures/FigureA04PanelD.gph", replace
graph export "Output/Figures/FigureA04PanelD.png", replace


***FIGURE A9 PANEL B

use Data/Analysis/Data_IndividualInit, clear

tab hospital

foreach x of varlist hospital urban large {
	foreach y of varlist Male Female Age_* AdvHIVInit UnderweightInit {
		reg `y' `x'
	}
}

corr large urban hospital

label define lhospital 0 "Regular Clinic" 1 "Hospital"
label values hospital lhospital
	
local date = 20240726
graph bar (mean) Age_09 AdvHIVInit UnderweightInit, over(hospital) legend(pos(6) rows(1) label(1 "Age {&le} 9") label(2 "Advanced HIV at Initiation") label(3 "Underweight at Initiation")) bar(1, color(blue)) bar(2, color(gs8)) bar(3, color(gs12))
		graph save "Output/Figures/FigureA09PanelB.gph", replace
		graph export "Output/Figures/FigureA09PanelB.png", replace

***TABLE A3


use Data/Analysis/Data_Individual, clear

gen Year=year(DeathDate)

gcollapse (max) Death, by(PatientId VisitSite Year)

drop if Year==.

collapse (sum) Death, by(VisitSite Year)


save Data/Prep/temp, replace

use Data/Analysis/Data_Individual, clear

gen Year=year(VisitDate)

collapse (max) New Return (mean) hospital urban, by(PatientId VisitSite Year EMRy)

gen InCare=0
replace InCare=1 if New==1
replace InCare=1 if Return==1

collapse (sum) New InCare Return (max) hospital urban, by(VisitSite Year EMRy)

merge 1:1 VisitSite Year using Data/Prep/temp
drop if _merge==2
drop _merge
replace Death=0 if Death==.


gsort VisitSite Year

gen InData=1

replace InCare=. if Year<EMRy
replace Return=. if Year<EMRy


gen AdoptedEMR=EMRy<=Year

gen NewAdopt=New if AdoptedEMR==1
gen DeathAdopt=Death if AdoptedEMR==1

gen SDNew=New
gen SDDeath=Death

gen hospitalEMR = (hospital*AdoptedEMR)
gen urbanEMR = (urban*AdoptedEMR)

collapse (sum) InData AdoptedEMR hospital hospitalEMR urban urbanEMR (mean) DeathAdopt NewAdopt New Death InCare Return (sd) SDNew SDDeath, by(Year)

replace New=. if Year==2019
replace NewA=. if Year==2019

replace InCare=. if Year==2019
replace Return=. if Year==2019

replace Death=. if Year==2019
replace DeathA=. if Year==2019

replace hospital=. if Year==2019
replace urban=. if Year==2019

replace hospitalEMR=. if Year==2019
replace urbanEMR=. if Year==2019

replace InData=106 if Year==2019
replace AdoptedEMR=106 if Year==2019

gen Adopted_ratio = (Adopted/InData)
gen hospitalEMR_ratio = (hospitalEMR/hospital)
gen urbanEMR_ratio = (urbanEMR/urban)

order Year InData Adopted_ratio hospitalEMR_ratio urbanEMR_ratio Death DeathA InCare New NewA Return

drop SDNew SDDeath

order Year InData Adopted_ratio hospitalEMR_ratio urbanEMR_ratio Death New DeathA InCare New NewA Return	
export excel Year InData Adopted_ratio hospitalEMR_ratio urbanEMR_ratio Death New DeathAdopt InCare NewAdopt Return using "Output/Tables/TableA03", firstrow(variables) replace